package com.zlx.sharelive.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.BaseInfoBean;
import com.zlx.module_base.base_api.res_data.MoneyHistoryBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_base.base_util.StringUt;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的钱包
 */
public class MyWalletAc extends BaseAc {

    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private BaseInfoBean mBaseInfoBean;
    private int page = 0;
    private List<MoneyHistoryBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<MoneyHistoryBean.InfoDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_my_wallet;
    }

    @Override
    public void initViews() {
        super.initViews();

        initRecyclerView();
        moneyHistory();
        getBaseInfo();
    }


    @Override
    protected void onStart() {
        super.onStart();

        moneyHistory();
        getBaseInfo();
    }

    /**
     * 用户基本信息
     */
    private void getBaseInfo() {
        ApiUtil.getUserApi().getBaseInfo(SPUtil.getToken(this)).
                observe(this, new BaseObserver<>(
                        new BaseObserverCallBack<ApiResponse<BaseInfoBean>>() {
                            @Override
                            public void onSuccess(ApiResponse<BaseInfoBean> data) {
                                if (null != data) {
                                    if (null != data.getData()) {
                                        if (data.getData().getCode() == 0) {
                                            mBaseInfoBean = data.getData();
                                            if (null != mBaseInfoBean.getInfo()) {
                                                if (mBaseInfoBean.getInfo().size() > 0) {
                                                    if (StringUt.isEmpty(mBaseInfoBean.getInfo().get(0).getMoney())) {
                                                        tv_money.setText("0");
                                                    } else {
                                                        tv_money.setText(mBaseInfoBean.getInfo().get(0).getMoney());
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();

                            }
                        }));
    }


    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<MoneyHistoryBean.InfoDTO.RecordsDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, MoneyHistoryBean.InfoDTO.RecordsDTO entity, int position) {
                if (entity.getDoType().equals("0") || entity.getDoType().equals("1")) {
                    holder.setText(R.id.tv_title, "购买影片");
                } else {
                    holder.setText(R.id.tv_title, entity.getRemark());
                }
                holder.setText(R.id.tv_time, entity.getCreateTime());
                holder.setText(R.id.tv_money, "- ￥" + entity.getMoney());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_wallet_details;
            }
        };

        rv_data.setAdapter(mAdapter);
    }

    /**
     * 钱包明细
     */
    private void moneyHistory() {
        ApiUtil.getMoneyApi().moneyHistory(SPUtil.getToken(this), String.valueOf(page)).observe(
                this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<MoneyHistoryBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<MoneyHistoryBean> data) {
                        mList.clear();
                        if (null != data) {
                            if (null != data.getData()) {
                                if (null != data.getData().getInfo()) {
                                    if (data.getData().getInfo().size() > 0) {
                                        if (null != data.getData().getInfo().get(0).getRecords()) {
                                            mList.addAll(data.getData().getInfo().get(0).getRecords());
                                        }
                                    }
                                }
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();

                    }
                }));
    }

    @OnClick({R.id.iv_back, R.id.ll_recharge, R.id.ll_withdrawal, R.id.tv_bank_card})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_bank_card:
                //银行卡管理
                startActivity(new Intent(MyWalletAc.this, BankCardListAc.class));
                break;
            case R.id.ll_recharge:
                startActivity(new Intent(MyWalletAc.this, RechargeAc.class));
                break;
            case R.id.ll_withdrawal:
                startActivity(new Intent(MyWalletAc.this, WithdrawalAc.class));
                break;

        }
    }
}
